﻿using Anke.SHManage.Model;
using Anke.SHManage.Model.BasicEventInfo;
using Anke.SHManage.Utility;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;

namespace Anke.SHManage.MSSQLDAL
{
     public class LoginDAL
    {

        /// <summary>
        /// 查询该登录人是否已有重复登录失败次数记录
        /// add2021-09-22 plq
        /// </summary>
        /// <param name="LoginName"></param>
        /// <returns></returns>
        public P_Login_Err GetLoginErrInfo(string LoginName)
        {
            try
            {
                using (SqlConnection con = new SqlConnection(SqlHelper.MainConnectionString))
                {
                    DataBase db = new DataBase(con);
                    P_Login_Err orginInfo = (from u in db.P_Login_Err
                                             where u.LoginName == LoginName 
                                             select u).FirstOrDefault();
                    if (orginInfo != null)
                    {
                        return orginInfo;
                    }
                    else
                    {
                        return null;
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtility.Error("LoginDAL/GetLoginErrInfo", ex.Message);
                return null;
            }
        }

        /// <summary>
        /// 限制登录时间已过，登录失败次数重新开始计数
        /// add2021-09-22 plq
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public bool LoginClearErrCount(P_Login_Err info)
        {
            using (DataBase db = new DataBase(SqlHelper.MainConnectionString))
            {
                try
                {
                    //调用隐式事务 TransactionScope 
                    using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
                    {
                        P_Login_Err orginInfo = (from u in db.P_Login_Err
                                                 where u.LoginName == info.LoginName
                                                 select u).FirstOrDefault();
                        if (orginInfo != null)
                        {
                            db.P_Login_Err.DeleteOnSubmit(orginInfo);
                            db.P_Login_Err.InsertOnSubmit(info);
                        }

                        //执行更新操作
                        db.SubmitChanges();
                        ts.Complete(); //提交事务
                        return true;
                    }
                }
                catch (Exception ex)
                {
                    LogUtility.Error("LoginDAL/LoginClearErrCount", ex.Message);
                    return false;
                }
            }
        }

        /// <summary>
        /// 用户登录成功，清除该用户的重复登录失败次数记录
        /// add2021-09-22 plq
        /// </summary>
        /// <param name="LoginName"></param>
        /// <returns></returns>
        public bool LoginClearErr(string LoginName)
        {
            try
            {
                using (SqlConnection con = new SqlConnection(SqlHelper.MainConnectionString))
                {
                    DataBase db = new DataBase(con);
                    P_Login_Err orginInfo = (from u in db.P_Login_Err
                                             where u.LoginName == LoginName 
                                                  select u).FirstOrDefault();
                    if (orginInfo != null)
                    {
                        db.P_Login_Err.DeleteOnSubmit(orginInfo);
                    }
                    //执行更新操作
                    db.SubmitChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                LogUtility.Error("LoginDAL/LoginClearErr", ex.Message);
                return false;
            }
        }

        /// <summary>
        /// 数据库记录一次用户登录失败次数
        /// add2021-09-22 plq
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public bool LoginErrCount(P_Login_Err info)
        {
            using (DataBase db = new DataBase(SqlHelper.MainConnectionString))
            {
                try
                {
                    //调用隐式事务 TransactionScope 
                    using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
                    {
                        P_Login_Err orginInfo = (from u in db.P_Login_Err
                                                 where u.LoginName == info.LoginName 
                                                 select u).FirstOrDefault();
                        if (orginInfo != null)
                        {
                            info.LoginErrNum = orginInfo.LoginErrNum + info.LoginErrNum; //登录失败次数+1
                            db.P_Login_Err.DeleteOnSubmit(orginInfo);
                        }
                        db.P_Login_Err.InsertOnSubmit(info);
                        //执行更新操作
                        db.SubmitChanges();
                        ts.Complete(); //提交事务
                        return true;
                    }
                }
                catch (Exception ex)
                {
                    LogUtility.Error("LoginDAL/LoginErrCount", ex.Message);
                    return false;
                }
            }
        }
    }
}
